package work.gac.unbind.v2;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.Indexed;


import java.util.*;

/**
 * @author Gong.Yang
 */
@Slf4j
public class MemberUtil {
    private static final String storeCode = "gac";


    /**
     * 通过CustomerId换取三方卡号
     */
    public static String getThridCode(String customerId, String baseUrl, String token) {
        JSONObject customer = getCustomer(customerId, baseUrl, token);

        return Optional.ofNullable(customer)
                .map(x -> x.getJSONObject("basicInfo"))
                .map(x -> x.getStr("thirdCode"))
                .orElse(null);

    }

    /**
     * 通过CustomerId换取用户
     */
    public static JSONObject getCustomer(String customerId, String baseUrl, String token) {
        String url = baseUrl + String.format(findCustomerUrlGet, customerId);
        HttpRequest request = HttpRequest.get(url).header("Authorization", token);
        return findPageFirst(request);
    }

    /**
     * 新 通过CustomerId换取用户
     */
    public static JSONObject getCustomerByID(String customerId, String baseUrl, String token) {
        String url = baseUrl + String.format(findCustomerByIdGet, customerId);
        HttpRequest request = HttpRequest.get(url).header("Authorization", token);
        String response = request.execute().body();
        JSONObject response_json = validJson(response);
        return response_json;
    }


    /**
     * 查询DL用户信息
     */
    public static JSONObject findDLCustomerInfo(String mobile, String baseUrl, String token) {
        String url = baseUrl + findDLCustomerInfoGet+mobile;
        String response = HttpRequest.get(url).header("Authorization", token).execute().body();
        return validJson(response);
    }

    /**
     * DL 解绑
     */
    public static void unbindByMobileOpenid(String mobile, String openId, String baseUrl, String token) {
        String url = baseUrl + String.format(unbindByMobileOpenidGet, mobile, openId);
        HttpResponse execute = HttpRequest.get(url).header("Authorization", token).execute();
//        System.out.println(execute);
    }


    /**
     * 查询分页的第一个数据
     */
    private static JSONObject findPageFirst(HttpRequest request) {
        String response = request.execute().body();
        JSONObject response_json = validJson(response);
        if (Objects.isNull(response_json)) {
            return null;
        }
        JSONArray content = response_json.getJSONArray("content");
        if (CollUtil.isEmpty(content)) {
            return null;
        }
        return content.getJSONObject(0);
    }


    private static JSONObject validJson(String response) {
        if (!JSONUtil.isTypeJSON(response)) {
            log.info("【响应非json】-》{}", response);
            return null;
        }
        return JSONUtil.parseObj(response);
    }


    //通过customerId查询用户
    public static final String findCustomerUrlGet = "/member-manage/store/" + storeCode + "/customer_info_data?page=0&size=10&expression=id+eq+%s";

    //新 通过customerID 查询用户
    public static final String findCustomerByIdGet = "/member-manage/store/" + storeCode + "/customer_info_data/%s";
    //DL 通过手机号获取用户DL信息
    public static final String findDLCustomerInfoGet = "/gateway/api/member-manage/store/" + storeCode + "/data_lake/customer/find/info/mobile/";//+手机号

    //DL 通过手机号和openid解绑用户
    public static final String unbindByMobileOpenidGet = "/gateway/api/member-manage/store/" + storeCode + "/data_lake/customer/unbind_by_mobile_openid/%s/%s";//{mobile}/{openId}


}
